package com.lzs.chuku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.chuku.mapper.UserMapper;
import com.lzs.chuku.service.OutorderService;
import com.lzs.entity.Outorder;
import com.lzs.entity.Pick;
import com.lzs.entity.Picktask;
import com.lzs.chuku.service.PickService;
import com.lzs.chuku.service.PicktaskService;
import com.lzs.entity.User;
import com.lzs.util.JwtUtils;
import com.lzs.util.TimeUtil;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/chuku/pick")
public class PickController {
    @Autowired
    private PickService pickService;
    @Autowired
    private PicktaskService picktaskService;
    @Autowired
    private OutorderService outorderService;
    @Autowired
    private UserMapper userMapper;
    @PostMapping("addpick")
    @ApiOperation("生成拣货")
    public R addpick(@RequestBody Pick pick, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId= JwtUtils.getMemberIdByJwtToken(token);
        User user = userMapper.findUserbyId(userId);
        try {
            String picktaskno = pick.getPicktaskno();
            QueryWrapper<Picktask> wrapper = new QueryWrapper<>();
            wrapper.eq("picktaskno", picktaskno);
            Picktask picktask = picktaskService.getOne(wrapper);
            pick.setPicktaskid(picktask.getId());
            pick.setWarehouseid(picktask.getWarehouseid());
            pick.setPickno("JH" + TimeUtil.gettime());
            pick.setState("0");
            pick.setCreateid(user.getUserId());
            pick.setCreator(user.getName());
            picktask.setReceiverid(user.getUserId());
            picktask.setReceivetime(new Date());
            picktask.setReceiver(user.getName());
            QueryWrapper<Picktask> wrapper1=new QueryWrapper<>();
            wrapper1.eq("picktaskno",picktaskno);
            picktaskService.update(picktask,wrapper1);
            pickService.saveOrUpdate(pick);
            List list=new ArrayList();
            list.add(picktask.getOutorderid());
            picktaskService.changeState(picktask.getPicktaskno(),"1");
            outorderService.changestate(list,"4");
            return R.ok().message("生成拣货成功");
        }catch (Exception e){
            return R.error().message("生成拣货失败");
        }
    }

    @PostMapping("findPickByPage/{currentPage}/{pageSize}")
    @ApiOperation("查询拣货条目")
    public R findPickState(@RequestBody Pick pick,
                           @PathVariable("currentPage") Integer currentPage,
                           @PathVariable("pageSize") Integer pageSIize){
        return pickService.findPickState(pick,currentPage,pageSIize);
    }

    @PostMapping("overPick")
    @ApiOperation("结束拣货")
    public R overPick(@RequestBody Pick pick){
        return pickService.overPick(pick);
    }

}

